package com.example.studywords.dlgs;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.fragment.app.DialogFragment;

import com.example.studywords.MainActivity;
import com.example.studywords.R;
import com.example.studywords.bean.TranslateBean;
import com.example.studywords.bean.WordBean;
import com.example.studywords.bean.WordInfoBean;
import com.example.studywords.sql.MyDatabase;
import com.example.studywords.utils.SqlUtil;

import java.util.List;

public class CheckDlg extends DialogFragment {
    private WordBean word;
    private String inputTranslation;
    public CheckDlg(WordBean word,String inputTranslation){
        this.word = word;
        this.inputTranslation = inputTranslation;
    }

    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("单词信息");
        LayoutInflater inflater = getActivity().getLayoutInflater();
        @SuppressLint("InflateParams")
        View view = inflater.inflate(R.layout.dlg_check, null);
        builder.setView(view);

        Context context = getContext();
        MainActivity mainActivity = (MainActivity) getActivity();

        TextView wordTextCheckDlg= view.findViewById(R.id.wordTextCheckDlg);
        TextView translationTextCheckDlg = view.findViewById(R.id.translationTextCheckDlg);
        //TODO:应该在WordInfo里加一个笔记功能
        TextView otherInfoTextCheckDlg = view.findViewById(R.id.otherInfoTextCheckDlg);
        TextView lastStudyTimeCheckDlg = view.findViewById(R.id.lastStudyTimeCheckDlg);
        TextView correctTimeCheckDlg = view.findViewById(R.id.correctTimeCheckDlg);

        //读单词
        wordTextCheckDlg.setText(word.getWordContent());
        //读wordInfo
        new Thread(()->{
            WordInfoBean wordInfoBean = MyDatabase.getDatabase(context).wordInfoDao().selectWordInfo(word.getWordId());
            otherInfoTextCheckDlg.setText(wordInfoBean.getWordNote());
            lastStudyTimeCheckDlg.setText(wordInfoBean.getLastStudyTime());
            correctTimeCheckDlg.setText(String.valueOf(wordInfoBean.getCorrectNum()));
        }).start();
        //将所有翻译连成一句话
        new Thread(()->{
            List<TranslateBean> translations = SqlUtil.selectTranslationByWordId(word.getWordId(),context);
            String result = "";
            for(TranslateBean translation:translations){
                result = result + (translation.getTranslationContent() + " ");
            }
            final String finalResult = result;
            mainActivity.runOnUiThread(()->translationTextCheckDlg.setText(finalResult));
        }).start();

        builder.setNegativeButton("错了",((dialogInterface, i) -> {
            new Thread(()->SqlUtil.updateWordInfo(word.getWordId(),false,context)).start();
        }));

        builder.setPositiveButton("也对",((dialogInterface, i) -> {
            new Thread(()->{
                SqlUtil.insertRelation(word.getWordContent(),"",inputTranslation,context);
                SqlUtil.updateWordInfo(word.getWordId(),true,context);
            }).start();

        }));

        return builder.create();
    }
}
